<template>
  <div class="container">

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span><el-button type="primary" @click="dialogVisible = true"> 上传文件 </el-button></span>
        <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
      </div>
      <div>
        <el-table :data="tableData" align="center" style="width: 100%" height="250" border fit highlight-current-row>
          <el-table-column fixed prop="docId" label="文件Id" width="150">
          </el-table-column>
          <el-table-column prop="uploadUserName" label="上传者昵称" width="180">
          </el-table-column>
          <el-table-column prop="uploadUserName" label="文件名称" width="180">
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-button type="primary" @click="enterEdit(scope.row.docId)"> 进入编辑 </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">

        <el-upload class="upload-demo" drag action="#" :auto-upload="true" :file-list="list" :before-upload="beforeUpload"
          :http-request="fileUpLoad" :on-preview="preview" :on-remove="onRemove">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="enterEditor()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>

import { upLoadFile , getPageData} from '@/api/editOnline/edit'
import { mapGetters } from 'vuex'
export default {
  name: 'Index',
  data() {
    return {
      list: [],
      dialogVisible: false,
      tableData: [
        {
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }
      ],
      fileId: undefined
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'userId'
    ])
  },
  mounted(){
    const data = {
      size: 10,
      currentPage: 1
    }
    getPageData(data)
    .then(res => {
        this.tableData = res.data.records
    })
    .catch(err => {})
  },
  methods: {
    enterEdit(docId){
      if(typeof docId !== 'undefined'){
        this.$router.push({ name: 'EditorOnline' ,params:{id: docId} })
      }
    },
    enterEditor() {
      if(typeof this.fileId === 'undefined'){
        this.$message({
          message: '文件上传为在线文档失败',
          type: 'warning'
        })
        return;
      }
      this.dialogVisible = false;
      this.$router.push({ name: 'EditorOnline' ,params:{id: this.fileId} })
    },
    beforeUpload(file) {
      this.list.push(file)
    },
    onRemove(file){
      
      const index = this.list.indexOf(file);
      
      if(index != -1){
        this.list.splice(index,1)
      }
      console.log(this.list)
    },
    fileUpLoad() {
      console.log(this.list);
      const that = this;
      const userInfo = {
        "name":this.name,
        "userId":this.userId
      };
      const lastUpload = that.list.length - 1;
      return upLoadFile(that.list[lastUpload], userInfo)
      .then(res => {
          if(res.code == 200 && res.data !== "null"){
            that.fileId = res.data
          }
      })
    }
  }
}
</script>

<style scoped="less">
.container {
  margin-top: 1%;
  margin-left: 1%;
  margin-right: 1%;

}
</style>